class Loot < ActiveRecord::Base
  belongs_to :mob
  belongs_to :item
  belongs_to :character
  belongs_to :raid
  validates_presence_of :character_id
  validates_presence_of :item_id
  validates_presence_of :raid_id
  validates_numericality_of :price

  def initialize(attributes = nil)
    if (attributes[:price].length <= 0)
      attributes[:price] = Item.find(attributes[:item_id]).default_price
    end
    super(attributes)
  end

  def self.total_value
    total = self.sum(:price, :include => :character, :conditions => [ "characters.active = ?", true ])
    if total.nil?
      total = 0.0
    end
    return total
  end

end
